package com.example.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.OperationLog;
import com.example.mapper.OperationLogMapper;
import com.example.service.IOperationLogService;
import com.example.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-10-07
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {

    private Logger logger = LoggerFactory.getLogger(OperationLogServiceImpl.class);

    @Autowired
    private OperationLogMapper operationLogMapper;

    public R saveOperationLog(OperationLog operationLog) {
        LocalDateTime createTime = LocalDateTime.now();
        operationLogMapper.insert(operationLog);
        return R.success();
    }


    public R deleteById(Integer id) {
        operationLogMapper.deleteById(id);
        return R.success();
    }

    public R deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            operationLogMapper.deleteById(id);
        }
        return R.success();
    }

    public R findPage(int currentPage, int pageSize, OperationLog operationLog) {
        System.out.println(operationLog.getStartTime()+operationLog.getEndTime());
        Page<OperationLog> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<OperationLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotBlank(operationLog.getIp()),OperationLog::getIp,operationLog.getIp());
        lambdaQueryWrapper.between(StrUtil.isNotBlank(operationLog.getStartTime())&&StrUtil.isNotBlank(operationLog.getEndTime()),OperationLog::getOperationTime,operationLog.getStartTime(),operationLog.getEndTime());
        lambdaQueryWrapper.like(StrUtil.isNotBlank(operationLog.getOperation()),OperationLog::getOperation,operationLog.getOperation());
        lambdaQueryWrapper.like(StrUtil.isNotBlank(operationLog.getOperator()),OperationLog::getIp,operationLog.getOperator());
        lambdaQueryWrapper.like(StrUtil.isNotBlank(operationLog.getType()),OperationLog::getType,operationLog.getType());
        lambdaQueryWrapper.like(StrUtil.isNotBlank(operationLog.getStatus()),OperationLog::getStatus,operationLog.getStatus());
        lambdaQueryWrapper.ne(OperationLog::getOperation,"日志管理");
        return R.success(operationLogMapper.selectPage(page, lambdaQueryWrapper));
    }


    public R findOne(Integer id) {
        LambdaQueryWrapper<OperationLog> lambdaQueryWrapper = new LambdaQueryWrapper<OperationLog>();
        lambdaQueryWrapper.eq(OperationLog::getId, id);
        return R.success(operationLogMapper.selectOne(lambdaQueryWrapper));
    }

    public R findAll() {
        LambdaQueryWrapper<OperationLog> lambdaQueryWrapper = new LambdaQueryWrapper<OperationLog>();
        return R.success(operationLogMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public R deleteAll() {
        operationLogMapper.delete(new LambdaQueryWrapper<>());
        return R.success();
    }

}
